/*
 * Copyright 2017-2025 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.annotation.processing.test.support

import java.io.File
import java.net.URLClassLoader
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi

/** Result of the compilation. */
@ExperimentalCompilerApi
sealed interface CompilationResult {
  /** The exit code of the compilation. */
  val exitCode: KotlinCompilation.ExitCode
  /** Messages that were printed by the compilation. */
  val messages: String
  /** Messages with captured diagnostic severity. */
  val diagnosticMessages: List<DiagnosticMessage>
  /** The directory where compiled files will be output to. */
  val outputDirectory: File
  /** Messages filtered by the given severities */
  fun messagesWithSeverity(vararg severities: DiagnosticSeverity): String =
    diagnosticMessages.filter { it.severity in severities }.joinToString("\n")
}

/** Result of the compilation */
@ExperimentalCompilerApi
class JvmCompilationResult(
  override val exitCode: KotlinCompilation.ExitCode,
  override val messages: String,
  override val diagnosticMessages: List<DiagnosticMessage>,
  private val compilation: KotlinCompilation,
) : CompilationResult {
  override val outputDirectory: File
    get() = compilation.classesDir

  /** ClassLoader to load the compiled classes */
  val classLoader =
    URLClassLoader(
      // Include the original classpaths and the output directory to be able to load classes from
      // dependencies.
      compilation.classpaths.plus(outputDirectory).map { it.toURI().toURL() }.toTypedArray(),
      this::class.java.classLoader
    )

  /** Compiled classes and resources output by the compilation. */
  val compiledClassAndResourceFiles: List<File> = outputDirectory.listFilesRecursively()

  /**
   * Intermediate source and resource files generated by the annotation processor that will be
   * compiled in the next steps.
   */
  val sourcesGeneratedByAnnotationProcessor: List<File> =
    compilation.kaptSourceDir.listFilesRecursively() +
      compilation.kaptKotlinGeneratedDir.listFilesRecursively()

  /** Stub files generated by kapt */
  val generatedStubFiles: List<File> = compilation.kaptStubsDir.listFilesRecursively()

  /**
   * The class, resource and intermediate source files generated during the compilation. Does not
   * include stub files and kapt incremental data.
   */
  val generatedFiles: Collection<File> =
    sourcesGeneratedByAnnotationProcessor + compiledClassAndResourceFiles + generatedStubFiles
}
